package com.yxhl.zoume.core.user.presenter.mywallet;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.TradeRecordsView;
import com.yxhl.zoume.data.http.model.base.BaseModel;
import com.yxhl.zoume.data.http.model.mywallet.TradeRecordBean;
import com.yxhl.zoume.data.http.rest.param.mywallet.TradeRecordsParam;
import com.yxhl.zoume.data.http.rest.response.mywallet.TradeRecordsResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.mywallet.TradeRecordsUseCase;

import java.util.List;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/14 .
 */

public class TradeRecordsPresenter extends BasePresenter implements IPresenter {
    /**
     * 一次上拉加载的item条目数
     */
    public static final int PAGE_SIZE =15;
    private  int currentPage=1;
    private int currentQueryType = 0;

    TradeRecordsView<BaseModel> mTradeRecordsView;

    @Inject
    TradeRecordsUseCase mTradeRecordsUseCase;

    @Inject
    public TradeRecordsPresenter(Context context) {
        super(context);
    }

    public void queryTradeRecords(int queryType, boolean isRefresh ){
        if (currentQueryType!=queryType){
            currentPage = 1;
        }
        if(isRefresh){
            currentPage = 1;
        }
        currentQueryType = queryType;
        TradeRecordsParam.PageBean pageBean = new TradeRecordsParam.PageBean();
        pageBean.setCurrentPage(currentPage++).setPageSize(PAGE_SIZE);
        TradeRecordsParam tradeRecordsParam = new TradeRecordsParam();
        tradeRecordsParam.setPage(pageBean)
                .setQueryType(queryType)
                .setMobile(getMobile())
                .setToken(getToken());


        mTradeRecordsUseCase.execute(tradeRecordsParam,new ZMSubscriber<TradeRecordsResponse>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onNext(TradeRecordsResponse tradeRecordsResponse) {
                        super.onNext(tradeRecordsResponse);
                        if (null != tradeRecordsResponse){
                            boolean isSuccess = tradeRecordsResponse.isSucc();
                            if (isSuccess){
                                List<TradeRecordBean> recordsList =
                                        tradeRecordsResponse.getData().getRecordsList();
                                mTradeRecordsView.renderTradeRecordsOnSuccess(recordsList);
                            }else{
                                mTradeRecordsView.renderTradeRecordsOnFailure(tradeRecordsResponse.getResultMessage());
                            }
                        }
                    }
                });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTradeRecordsUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        mTradeRecordsView = (TradeRecordsView<BaseModel>) view;

    }
}
